<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>将格式化后的字符串写入到流</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.fprintf" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">fprintf</h1>
  <p class="verinfo">(PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">fprintf</span> &mdash; <span class="dc-title">将格式化后的字符串写入到流</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.fprintf-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>fprintf</strong></span>
    ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$args</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span>
  ]] ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   写入一个根据 <code class="parameter">format</code> 格式化后的字符串到
   由 <code class="parameter">handle</code> 句柄打开的流中。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.fprintf-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">handle</code></dt>

     <dd>

      <p class="para">文件系统指针，是典型地由
<span class="function"><a href="fopen.html" class="function">fopen()</a></span> 创建的 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>(资源)。</p>
     </dd>

    
    
     <dt>
<code class="parameter">format</code></dt>

     <dd>

      <p class="para">
       参见 <span class="function"><a href="sprintf.html" class="function">sprintf()</a></span> 中对 
       <code class="parameter">format</code> 的描述。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">args</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">...</code></dt>

     <dd>

      <p class="para">
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.fprintf-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回写入的字符串长度。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.fprintf-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5906">
    <p><strong>Example #1 <span class="function"><strong>fprintf()</strong></span>: zero-padded integers</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(!(</span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'date.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return;<br />}<br /><br /></span><span style="color: #0000BB">fprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"%04d-%02d-%02d"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$year</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$month</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$day</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;will&nbsp;write&nbsp;the&nbsp;formatted&nbsp;ISO&nbsp;date&nbsp;to&nbsp;date.txt<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
   <div class="example" id="example-5907">
    <p><strong>Example #2 <span class="function"><strong>fprintf()</strong></span>: formatting currency</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(!(</span><span style="color: #0000BB">$fp&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'currency.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'w'</span><span style="color: #007700">)))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return;<br />}<br /><br /></span><span style="color: #0000BB">$money1&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">68.75</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">54.35</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$money&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$money1&nbsp;</span><span style="color: #007700">+&nbsp;</span><span style="color: #0000BB">$money2</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;echo&nbsp;$money&nbsp;will&nbsp;output&nbsp;"123.1";<br /></span><span style="color: #0000BB">$len&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fprintf</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'%01.2f'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$money</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;will&nbsp;write&nbsp;"123.10"&nbsp;to&nbsp;currency.txt<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">"wrote&nbsp;</span><span style="color: #0000BB">$len</span><span style="color: #DD0000">&nbsp;bytes&nbsp;to&nbsp;currency.txt"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;use&nbsp;the&nbsp;return&nbsp;value&nbsp;of&nbsp;fprintf&nbsp;to&nbsp;determine&nbsp;how&nbsp;many&nbsp;bytes&nbsp;we&nbsp;wrote<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.fprintf-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - 输出格式化字符串</span></li>
    <li class="member"><span class="function"><a href="sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li>
    <li class="member"><span class="function"><a href="sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a> - 根据指定格式解析输入的字符</span></li>
    <li class="member"><span class="function"><a href="fscanf.html" class="function" rel="rdfs-seeAlso">fscanf()</a> - 从文件中格式化输入</span></li>
    <li class="member"><span class="function"><a href="vsprintf.html" class="function" rel="rdfs-seeAlso">vsprintf()</a> - 返回格式化字符串</span></li>
    <li class="member"><span class="function"><a href="number_format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - 以千位分隔符方式格式化一个数字</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>